package com.zs.admin.service.wisdbook.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.zs.admin.validate.commons.PageValidate;
import com.zs.admin.service.wisdbook.IWisdbookService;
import com.zs.admin.validate.wisdbook.WisdbookCreateValidate;
import com.zs.admin.validate.wisdbook.WisdbookUpdateValidate;
import com.zs.admin.validate.wisdbook.WisdbookSearchValidate;
import com.zs.admin.vo.wisdbook.WisdbookListedVo;
import com.zs.admin.vo.wisdbook.WisdbookDetailVo;
import com.zs.admin.vo.wisdbook.WisdbookStaticsVo;
import com.zs.common.core.PageResult;
import com.zs.common.entity.wisdbook.Wisdbook;
import com.zs.common.entity.wisdbook.WisdbookPage;
import com.zs.common.entity.wisdbook.WisdbookPageMark;
import com.zs.common.mapper.WisdbookMapper;
import com.zs.common.mapper.WisdbookPageMapper;
import com.zs.common.mapper.WisdbookPageMarkMapper;
import com.zs.common.util.StringUtils;
import com.zs.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 智书实现类
 * @author SwAdmin
 */
@Service
public class WisdbookServiceImpl implements IWisdbookService {
        
    @Resource
    WisdbookMapper wisdbookMapper;

    @Resource
    WisdbookPageMarkMapper wisdbookPageMarkMapper;


    /**
     * 智书列表
     *
     * @author SwAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<WisdbookListedVo>
     */
    @Override
    public PageResult<WisdbookListedVo> list(PageValidate pageValidate, WisdbookSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        MPJQueryWrapper<Wisdbook> queryWrapper = new MPJQueryWrapper<>();
        queryWrapper.select("t.*","c.nickname as markUserName");
        queryWrapper.leftJoin("tb_wisdbook_user b on t.id = b.book_id ");
        queryWrapper.leftJoin("tb_system_auth_admin c on b.mark_user_id = c.id");
        queryWrapper.eq("t.is_delete", 0);
        queryWrapper.orderByDesc("t.id");

        wisdbookMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:id:int",
            "like:name:str",
        });

        IPage<WisdbookListedVo> iPage = wisdbookMapper.selectJoinPage(new Page<>(page, limit), WisdbookListedVo.class,queryWrapper);

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    /**
     * 智书详情
     *
     * @author SwAdmin
     * @param id 主键参数
     * @return Wisdbook
     */
    @Override
    public WisdbookDetailVo detail(Integer id) {
        Wisdbook model = wisdbookMapper.selectOne(
                new QueryWrapper<Wisdbook>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        WisdbookDetailVo vo = new WisdbookDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    @Override
    public WisdbookStaticsVo statics(Integer id) {




        return null;
    }

    /**
     * 智书新增
     *
     * @author SwAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(WisdbookCreateValidate createValidate) {
        Wisdbook model = new Wisdbook();
        model.setName(createValidate.getName());
        model.setGrade(createValidate.getGrade());
        model.setSubject(createValidate.getSubject());
        model.setSection(createValidate.getSection());
        model.setEdition(createValidate.getEdition());
        model.setVolume(createValidate.getVolume());
        model.setCover(UrlUtils.toRelativeUrl2(createValidate.getCover()));
        model.setPdfUrl(UrlUtils.toRelativeUrl2(createValidate.getPdfUrl()));
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setPid(createValidate.getPid());
        wisdbookMapper.insert(model);
    }

    /**
     * 智书编辑
     *
     * @author SwAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(WisdbookUpdateValidate updateValidate) {
        Wisdbook model = wisdbookMapper.selectOne(
                new QueryWrapper<Wisdbook>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));


        if(model.getPid() == null&& updateValidate.getMarkStatus()!=null&&updateValidate.getMarkStatus()==2){
            List<Wisdbook>  models = wisdbookMapper.selectList(
                            new QueryWrapper<Wisdbook>()
                            .eq("pid",  model.getId())
                            .eq("is_delete", 0));

            if (models.size()>0){
                for (Wisdbook wd:models) {
                    if(wd.getMarkStatus()!=2){
                        Assert.notNull(null, "子书未标注完成");
                    }
                }
            }
        }

        if(model.getPid()!=null&& updateValidate.getMarkStatus()!=null&& updateValidate.getMarkStatus()==2){

            Long count = wisdbookPageMarkMapper.selectCount(
                    new QueryWrapper<WisdbookPageMark>()
                            .eq("book_id",  model.getId())
                            .eq("is_delete", 0)
                            );

            if(count==0){
                Assert.notNull(null, "本书没有发现标注");
            }
        }


        Assert.notNull(model, "数据不存在!");
        String cover = UrlUtils.toRelativeUrl2(updateValidate.getCover());
        String pdfUrl = UrlUtils.toRelativeUrl2(updateValidate.getPdfUrl());

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setGrade(updateValidate.getGrade());
        model.setSubject(updateValidate.getSubject());
        model.setSection(updateValidate.getSection());
        model.setEdition(updateValidate.getEdition());
        model.setVolume(updateValidate.getVolume());
        model.setCover(StringUtils.isEmpty(cover)?null:cover);
        model.setPdfUrl(StringUtils.isEmpty(pdfUrl)?null:pdfUrl);
        model.setMarkStatus(updateValidate.getMarkStatus());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setPid(updateValidate.getPid());
        wisdbookMapper.updateById(model);
    }

    /**
     * 智书删除
     *
     * @author SwAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Wisdbook model = wisdbookMapper.selectOne(
                new QueryWrapper<Wisdbook>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        wisdbookMapper.updateById(model);
    }

}
